package com.example.winner.contextmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = new TextView(this);
        tv.setText("上下文菜单的载体");
        registerForContextMenu(tv);
        setContentView(tv);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuInfo)
    {
        menu.add(0,1,1,R.string.New);
        menu.add(0,2,2,R.string.Open);
        super.onCreateContextMenu(menu,view,menuInfo);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);
        if(item.getItemId()==1)
        {
            finish();
        }
        if(item.getItemId()==2)
        {
            finish();
        }
        return super.onContextItemSelected(item);
    }
}
